<?php
/**
 * Created by PhpStorm.
 * User: wuxiaojie
 * Date: 2018/6/12
 * Time: 22:19
 */

if (!defined('IN_ANWSION')) {
    die;
}

class cart_class extends AWS_MODEL
{
//    public function update_views($uid)
//    {
//        if (AWS_APP::cache()->get('update_views_people_' . md5(session_id()) . '_' . intval($uid))) {
//            return false;
//        }
//
//        AWS_APP::cache()->set('update_views_people_' . md5(session_id()) . '_' . intval($uid), time(), get_setting('cache_level_normal'));
//
//        return $this->query('UPDATE ' . $this->get_table('users') . ' SET views_count = views_count + 1 WHERE uid = ' . intval($uid));
//    }


    /**
     * @param $uid
     * 获取用户购物车
     */
    public function get_cart_list_by_uid($uid){

        if ($user_cart = $this->fetch_row('user_cart', 'uid = ' . intval($uid)))
        {

            if($user_cart['cart_content']){
                $user_cart['cart_content'] = json_decode($user_cart['cart_content'],true);
            }
            return $user_cart;
        }else{
            return false;
        }
    }

    /**
     * @param $uid
     * @param $cart_content
     * @return bool|int
     */
    public function insert_goods($uid,$cart_content)
    {

        if (!$uid OR !$cart_content) {
            return false;
        }

        $cart_id = $this->insert('user_cart', array(
            'uid' => $uid,
            'cart_content' => $cart_content,
            'add_time' => time()
        ));

        return $cart_id;

    }

    /**
     * @param $update_data
     * @param $cid
     * @return bool|int
     * @throws Zend_Exception
     */
    public function update_cart_fields($update_data, $cid)
    {

        if (!$update_data OR !$cid) {
            return false;
        }
        if(!is_array($update_data)){
            return false;
        }
        $update_data['cart_content'] = jsonp_encode($update_data['cart_content'],true);
        $update_data['update_time'] = time();
        return $this->update('user_cart', $update_data, 'cart_id = ' . intval($cid));

    }




}